#include<iostream>
#include<sw/redis++/redis++.h>
#include<vector>
#include<string>
#include<thread>
#include<chrono>
#include<set>
#include<unordered_map>
#include"Util.hpp"
using namespace std;
using namespace chrono_literals;


void test1(sw::redis::Redis& redis)
{
      cout << "zadd" << endl;
      redis.flushall();

      //一次设置一对
      redis.zadd("key","jack",99);
      //一次设置多对
      vector<pair<string,double>> vp = {
        std::make_pair("mike",93),
        std::make_pair("sora",88),
        std::make_pair("amy",96)
      };

      redis.zadd("key",vp.begin(),vp.end());

      //获取元素
      vector<string> members;
      redis.zrange("key",0,-1,std::back_inserter(members));
      printContainer(members);

      vector<pair<string,double>> all;
      redis.zrange("key",0,-1,std::back_inserter(all));
      printPair(all);
}

void test2(sw::redis::Redis& redis)
{
      cout << "zcard" << endl;
      redis.flushall();

      redis.zadd("key","jack",99);
      redis.zadd("key","sora",89);
      redis.zadd("key","tom",79);
      redis.zadd("key","amy",92);
      
      cout << redis.zcard("key") << endl; 
}

void test3(sw::redis::Redis& redis)
{
      cout << "zrem" << endl;
      redis.flushall();

      redis.zadd("key","jack",99);
      redis.zadd("key","sora",89);
      redis.zadd("key","tom",79);
      redis.zadd("key","amy",92);

      //一次删除一个
      auto ret = redis.zrem("key","jack");
      cout << "ret: " << ret << endl;
      cout << "第一次删除后剩下长度" << redis.zcard("key") << endl; 
      //一次删除多个(迭代器/初始化列表)
      ret = redis.zrem("key",{"jack","amy","mike"});
      cout << "ret: " << ret << endl;
      cout << "第二次删除后剩下长度" << redis.zcard("key") << endl; 
}

void test4(sw::redis::Redis& redis)
{
      cout << "zscore" << endl;
      redis.flushall();

      redis.zadd("key","jack",99);
      redis.zadd("key","sora",89);
      redis.zadd("key","tom",79);
      redis.zadd("key","amy",92);

      auto score = redis.zscore("key","a");
      if(score) cout << score.value() << endl;
      else cout << "score无效"  << endl;
}

void test5(sw::redis::Redis& redis)
{
      cout << "zrank" << endl;
      redis.flushall();

      redis.zadd("key","jack",99);
      redis.zadd("key","sora",89);
      redis.zadd("key","tom",79);
      redis.zadd("key","amy",92);

       auto score = redis.zrank("key","s");
      if(score) cout << score.value() << endl;
      else cout << "rank无效"  << endl;
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    test5(redis);
    return 0;
}